
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//@WebServlet("/aaa")
public class DataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String p = request.getParameter("p");
		if (p != null) {
			String sp = URLEncoder.encode(p.toUpperCase());
			
			String s1 = HttpRequest.sendGet("http://apidatatest.datatang.com/data/credit/queryNightTravelProCnt",
					"apikey=35f23263d922e464ac872b54e1d25f46&rettype=json&carlicense="+sp+"&starttime=201504&endtime=201603");
			String yeS=getS(s1, "nighttravetimes");

			String s2 = HttpRequest.sendGet("http://apidatatest.datatang.com/data/credit/queryOverSpeedCnt",
					"apikey=35f23263d922e464ac872b54e1d25f46&rettype=json&carlicense="+sp+"&starttime=201504&endtime=201603");

			String chaosuS=getS(s2, "overspeedtimes");

			String s3 = HttpRequest.sendGet("http://apidatatest.datatang.com/data/credit/queryLongtimeDrive",
					"apikey=35f23263d922e464ac872b54e1d25f46&rettype=json&carlicense="+sp+"&starttime=201504&endtime=201603");
			
			String pilaoS=getS(s3, "longtimes");
			
			String res=chaosuS+","+pilaoS+","+yeS;

			PrintWriter pw = response.getWriter();
			try {
				pw.write(res);
				pw.close();
			} catch (Exception e) {

			}
		}

	}

	public static String getS(String str, String f) {
		if (str != null && str.contains(f)) {
			int i = str.indexOf(f) + f.length() + 2;
			String subs = str.substring(i, i + 2);
			return subs;
		}
		return "0";
	}

}
